package com.huiminpay.merchant;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @ClassName: MerchantApplicationBootstrap
 * @Package: com.huiminpay.merchant
 * @Description: 启动类
 * @Author 谢同学
 * @Create 2023/11/15 15:02
 * @Version 1.0
 */
@SpringBootApplication
public class MerchantApplicationBootstrap {
    public static void main(String[] args) {
        SpringApplication.run(MerchantApplicationBootstrap.class,args);
    }

    //restTemplate默认依赖jdk提供http连接的能力（HttpURLConnection），但是OkHttp性能更高
    @Bean
    public RestTemplate restTemplate(){
        //设置RestTemplate底层的客户端工具为okhttp3
        RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());
        //解决 String 乱码问题
        List<HttpMessageConverter<?>> httpMessageConverters = restTemplate.getMessageConverters();
        //设置编码格式
        httpMessageConverters.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        restTemplate.setMessageConverters(httpMessageConverters);

        return restTemplate;
    }


}
